package com.oauth.global.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.oauth.auth.client.beans.MemberClientBean;
import com.oauth.auth.client.rpc.MemberRemoteService;
import com.oauth.auth.client.rpc.MemberRemoteServiceAsync;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.global.client.rpc.GlobalRemoteService;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;
import com.oauth.global.client.ui.BirthdayNotification;
import com.oauth.global.client.ui.StatusUpdate;
import com.oauth.layout.client.StandardLayout;

public class GlobalEntryPoint implements EntryPoint {

	private final MemberRemoteServiceAsync memberService = GWT.create(MemberRemoteService.class);

	private final GlobalRemoteServiceAsync globalService = GWT.create(GlobalRemoteService.class);

	private static GlobalEntryPointUiBinder uiBinder = GWT.create(GlobalEntryPointUiBinder.class);

	interface GlobalEntryPointUiBinder extends UiBinder<StandardLayout, GlobalEntryPoint> {
	}

	@UiField
	TabPanel tabPanel;

	public void onModuleLoad() {
		final StandardLayout p = uiBinder.createAndBindUi(this);
		// header.getLoadingLabel().setText("loading...");
		memberService.getMember(new OAuthAsnyCallBack<MemberClientBean>() {
			public void onSuccess(MemberClientBean result) {
				p.setHeaderUserName(result.getEmail());
				p.initMyApplications();

				final StatusUpdate statusUpdate = new StatusUpdate();
				statusUpdate.setService(globalService);
				final BirthdayNotification birthdayNotification = new BirthdayNotification();
				birthdayNotification.setService(globalService);
				tabPanel.add(statusUpdate, "Status Update");
				tabPanel.add(birthdayNotification, "Upcoming Birthday's");

				tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {

					@Override
					public void onSelection(SelectionEvent<Integer> pEvent) {
						int index = pEvent.getSelectedItem();
						switch (index) {
						case 0:
							statusUpdate.initImages();
							break;
						case 1:
							birthdayNotification.initBirthdays();
							break;
						}
					}
				});
			}
		});

		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(p);
		// RootPanel.get().add(dockPanel);
	}

}